﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Log.Views
{
    /// <summary>
    /// ItemControlView.xaml 的交互逻辑
    /// </summary>
    public partial class ItemControlView : UserControl
    {
        public ItemControlView()
        {
            InitializeComponent();      
        }
        //打开最近记录
        private void RecentInformation(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                FileInfo[] fileInfos = new DirectoryInfo(string.Format(@"{0}\数据\{1}\", Environment.CurrentDirectory, "Log")).GetFiles();
                //使用Linq查询高效便捷
                IEnumerable<FileInfo> fileInfos1 = from fileInfo in fileInfos
                                                   where fileInfo.Extension.Contains("csv") && fileInfo.Name.Contains(this.Name)
                                                   select fileInfo;

                FileInfo fileInfo2 = fileInfos1.OrderBy(fileInfo1 => fileInfo1.LastWriteTime).Last();

                if (string.IsNullOrEmpty(fileInfo2.FullName)) { MessageBox.Show("不存在记录", "提示"); return; }
                var rows = ReadCSV(fileInfo2.FullName);
                txtBox.Clear();
                var lines = rows.Reverse().Take<string>(1000).Reverse();
                foreach (var v in lines)
                { txtBox.AppendText(v + "\r\n"); }
                GoodExperience();
            }
            catch (Exception ex)
            {
                //NotifyG.Error(ex.ToString());
                MessageBox.Show(ex.Message);
            }
        }

        public string[] ReadCSV(string filePath)
        {
            try
            {
                //FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                StreamReader sr = new StreamReader(filePath, Encoding.Default);
                string fileContent = sr.ReadToEnd();
                sr.Close();
                //fs.Close();
                sr.Dispose();
                //fs.Dispose();
                return fileContent.Replace("\r\n", ";").Split(';');
            }
            catch (Exception ex)
            {
                //NotifyG.Error(ex.ToString());
                return null;
            }
        }
        //查找文件目录
        private void FindFile(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(string.Format(@"{0}\数据\Log\", Environment.CurrentDirectory));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ClearTxt(object sender, System.Windows.RoutedEventArgs e)
        {
            txtBox.Clear();
            GoodExperience();
        }

        /// <summary>
        /// 通过事件添加信息
        /// </summary>
        /// <param name="mes"></param>
        public void AppendText(string mes)
        {
            this.txtBox.Dispatcher.BeginInvoke((Action)delegate
            {
                txtBox.AppendText(mes + "\r\n");
                GoodExperience();
            });
        }

        private void GoodExperience()
        {
            txtBox.ScrollToEnd();
            //返回-1
            //CountRow.Text = "行数：" + (txtBox.LineCount-1);
        }
    }
}
